

set more off
clear all
set niceness 10 
set processor 4

*** Paths ****
gl path1 = "/Users/Wei/Dropbox/Census"
gl path2 = "/Users/Wei/Dropbox/Fertility/Workingdata"
gl path3 = "/Users/Wei/Dropbox/Fertility/Results"
gl path4 = "/Users/Wei/Dropbox/Fertility/Figures"


log using "$path3/child_results.log", replace 

************ Fertility **************
set more off

use "$path2/marr_policy", clear
gen mort_rate = 100 -sur_rate if women == 1 
ren year yr_t
gen year = year_birth +15
merge m:1 prov year using "$path2/fines_economic", keepusing(lngdp_pc lngdp secondary primary)
drop if _merge == 2
drop _merge 
drop year
ren yr_t year

egen fine_7_15 = rowmean(fine_age7-fine_age15)
egen fine_8_15 = rowmean(fine_age8-fine_age15)
egen fine_6_20 = rowmean(fine_age6-fine_age20)
egen fine_16_22 = rowmean(fine_age6-fine_age20)
egen fine_8_22 = rowmean(fine_age8-fine_age22)
egen fine_26_30 = rowmean(fine_age26-fine_age30)
egen fine_21_30 = rowmean(fine_age21-fine_age30)

egen fine_6_18 = rowmean(fine_age6-fine_age18)
egen fine_6_22 = rowmean(fine_age6-fine_age22)
egen fine_8_20 = rowmean(fine_age8-fine_age20)
egen fine_4_20 = rowmean(fine_age4-fine_age20)
egen fine_1_20 = rowmean(fine_age1-fine_age20)

egen fine_4_22 = rowmean(fine_age4-fine_age22)

gl FINE_ALL = "fine_6_15 fine_16_20 fine_21_25 fine_26_30"
gl FINE_VAR_4 =  "fine_6_15 fine_16_20 fine_21_30"

gl FINE_VAR_3 =  "fine_6_15 fine_16_20 fine_21_25"
gl FINE_VAR_1 = "fine_6_20"

replace nbroth = 99 if mi(nbroth)
replace nsister = 99 if mi(nsister)

gen lnsec = ln(secondary)
gen lnpri = ln(primary)
gen lnsec_mi = mi(lnsec)
gen lnpri_mi = mi(lnpri)
egen mean_lnsec = mean(lnsec)
egen mean_lnpri = mean(lnpri)
replace lnsec = mean_lnsec if mi(lnsec)
replace lnpri = mean_lnpri if mi(lnpri)
la var fine_6_10 "Ages 6-10"
la var fine_11_15 "Ages 11-15"
la var fine_16_20 "Ages 16-20"
la var fine_21_25 "Ages 21-25"
la var fine_6_12 "Ages 6-12"
la var fine_13_19 "Ages 13-19"
la var fine_20_25 "Ages 20-25"

gen provid = prov 
merge m:1 provid using "$path2/Comp_Law", keepusing(law_*) nogen 

cap drop elig
gen elig = 1 if year_birth >= law_year - 6 
replace elig = 0 if year_birth <= law_year - 16
replace elig = (16-(law_year - year_birth ))/10 if law_year - 16<year_birth & year_birth<law_year - 6 
drop provid


gl CONTROL_1 = "women#prov##c.year_birth women#year#year_birth"
gl CONTROL_2 = "women#prov##c.year_birth women#year#year_birth women#prov#year"

gen urban_hk = hktype == 2 
gen urban_res = city_flag <= 2 if !mi(city_flag)
replace urban_res = urban_hk if  mi(urban_res)


egen han_p = mean(han), by(year hhid prov region)

replace high_occ = 0 if high_occ == . & !mi(work)


drop if han_p >0 & han_p < 1 // drop mixed households
drop if women == 0 
set more off
gen n_death = n_birth - n_survive
replace n_death = . if n_death < 0
replace n_birth = 0 if married_ever == 0 
replace n_mbirth = 0 if married_ever == 0  
replace n_fbirth = 0 if married_ever == 0  
replace n_survive = 0 if married_ever == 0 
replace n_mchild = 0 if married_ever == 0 
replace n_fchild = 0 if married_ever == 0 
drop if mi(n_birth)
drop if mi(n_death)

gen male_prop = n_mbirth/n_birth

* Table C1, Panel A 
su n_birth [aw = wt]
su n_birth if n_birth >= 1 [aw = wt]
su n_mbirth if n_birth >= 1 [aw = wt]

* Table C2, column 1 
cap erase "$path3/tab_c2_part1.xls"
cap erase "$path3/tab_c2_part1.txt"
reghdfe n_birth women#c.(fine_6_20) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c2_part1.xls", dec(3) append 


* Table C3, Column 6
cap erase "$path3/tab_c3_part2.xls"
cap erase "$path3/tab_c3_part2.txt"
set more off
foreach y_var in  "n_birth" {
reghdfe `y_var' women#c.(fine_6_18) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part2.xls", dec(3) append 
reghdfe `y_var' women#c.(fine_6_22) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part2.xls", dec(3) append 
reghdfe `y_var' women#c.(fine_8_20) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part2.xls", dec(3) append 
reghdfe `y_var' women#c.(fine_4_20) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part2.xls", dec(3) append 
reghdfe `y_var' women#c.(fine_1_20) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part2.xls", dec(3) append 
reghdfe `y_var' women#c.(fine_4_22) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part2.xls", dec(3) append 
}

* Table C6, Column 1 
reghdfe n_birth  fine_6_15 fine_16_20 fine_21_25  if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c6_part1.xls", dec(3) replace 


* Table C8, Column 1
reghdfe n_birth  fine_6_20  if han_p == 0  [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c8_part1.xls", dec(3) replace 


* Table C9, Row G
cap erase "$path3/tab_c9_g.xls"
cap erase "$path3/tab_c9_g.txt"
reghdfe n_birth fine_6_20  if han & urban_hk == 1 [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c9_g.xls", dec(3) append 
reghdfe n_birth fine_6_20  if han & urban_hk != 1 [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c9_g.xls", dec(3) append 



************ Child Mortality *********
use "$path2/child_gender_mort", clear 
gl CONTROL_1 = "prov##c.year_birth year#year_birth"
gl CONTROL_2 = "prov##c.year_birth year#year_birth prov#year"
gen wt = 1 
replace wt = 4 if year == 2005
egen n = sum(1), by(hhid)

replace die = die*100

* Table C1 Panel B Row 1 
su die [aw = wt]

* Table C2, Column (2)
reghdfe die  fine_6_20  if han_p == 1 [aw = wt], a($CONTROL_1) cluster(prov)

* Table C3, Column (7)
cap erase "$path3/tab_c3_part3.xls"
cap erase "$path3/tab_c3_part3.txt"

foreach sample in "han_p == 1"{
reghdfe die  fine_6_18  if `sample' [aw = wt], a($CONTROL_1) cluster(prov)
su die if `sample' [aw = wt] 
outreg2 using "$path3/tab_c3_part3.xls",append adds(Mean, `r(mean)') dec(3)
reghdfe die  fine_6_22  if `sample' [aw = wt], a($CONTROL_1) cluster(prov)
su die if `sample' [aw = wt] 
outreg2 using "$path3/tab_c3_part3.xls",append adds(Mean, `r(mean)') dec(3)
reghdfe die  fine_8_20  if `sample' [aw = wt], a($CONTROL_1) cluster(prov)
su die if `sample' [aw = wt] 
outreg2 using "$path3/tab_c3_part3.xls",append adds(Mean, `r(mean)') dec(3)
reghdfe die  fine_4_20  if `sample' [aw = wt], a($CONTROL_1) cluster(prov)
su die if `sample' [aw = wt] 
outreg2 using "$path3/tab_c3_part3.xls",append adds(Mean, `r(mean)') dec(3)
reghdfe die  fine_1_20  if `sample' [aw = wt], a($CONTROL_1) cluster(prov)
su die if `sample' [aw = wt] 
outreg2 using "$path3/tab_c3_part3.xls",append adds(Mean, `r(mean)') dec(3)
reghdfe die  fine_4_22  if `sample' [aw = wt], a($CONTROL_1) cluster(prov)
su die if `sample' [aw = wt] 
outreg2 using "$path3/tab_c3_part3.xls",append adds(Mean, `r(mean)') dec(3)
}

* Table C6 Column 2 
reghdfe die c.(fine_6_15 fine_16_20 fine_21_25) if  han_p == 1 [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c6_part2.xls", dec(3) replace 

* Table C8, Column 2
reghdfe die  fine_6_20  if han_p == 0 [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c8_part2.xls", dec(3) replace 


* Table C9, Row H
cap erase "$path3/tab_c9_h.xls"
cap erase "$path3/tab_c9_h.txt"
gen rural = 1 - urban_hk
reghdfe die  fine_6_20  if han_p == 1 & urban_hk [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c9_h.xls", dec(3) append 
reghdfe die  fine_6_20  if han_p == 1 & rural [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c9_h.xls", dec(3) append 

replace die = die/100


***** Child Education ****

use "$path2/child_edu", clear 
gl CONTROL_MOTHER = "girl#prov##c.(m_birth_year) girl#m_birth_year#year girl#age#year"
gl CONTROL_GIRL = "girl#prov##c.(birth_year) girl#birth_year#year"

gl CONTROL_BOTH = "girl#prov##c.(m_birth_year birth_year) girl#m_birth_year#year girl#birth_year#year"

gl CONTROL_SHORT = "girl#prov#m_birth_year#year girl#prov##c.(birth_year) girl#birth_year#year"
gl CONTROL_LONG = "girl#prov#birth_year#year girl#prov##c.(m_birth_year) girl#m_birth_year#year"

* Table C1, Panel B, Rows 2 & 3 
su elig_edu educ [aw = wt]

* Table C2, Columns 3 - 4 
reghdfe elig_edu  fine_6_20  if han_hh == 1 [aw = wt], a($CONTROL_MOTHER) cluster(prov)
reghdfe educ  fine_6_20  if han_hh == 1 [aw = wt], a($CONTROL_MOTHER) cluster(prov)


* Table C3, Columns 8-9
cap erase "$path3/tab_c3_part4.xls"
cap erase "$path3/tab_c3_part4.txt"
foreach sample in "han_hh == 1" {
foreach y_var in  "elig_edu""educ" {
foreach x_var in "fine_1_20" "fine_6_18""fine_6_22" "fine_8_20" "fine_4_20" "fine_4_22"{
reghdfe `y_var' c.(`x_var') if  `sample' [aw = wt], a($CONTROL_MOTHER) cluster(prov)
su `y_var' if  `sample' [aw = wt]
outreg2 using "$path3/tab_c3_part4.xls", dec(3) append adds(mean, `r(mean)')
}
}
}

* Table C6, Columns 3-4
cap erase "$path3/tab_c6_part3.xls"
cap erase "$path3/tab_c6_part3.txt"
foreach y_var in  "elig_edu""educ" {
reghdfe `y_var' c.(fine_6_15 fine_16_20 fine_21_25) if  han_hh == 1 [aw = wt], a($CONTROL_MOTHER) cluster(prov)
su `y_var' if  han_hh == 1 [aw = wt]
outreg2 using "$path3/tab_c6_part3.xls", dec(3) append adds(mean, `r(mean)')
}

* Table C8, Columns 3-4
cap erase "$path3/tab_c8_part3.xls"
cap erase "$path3/tab_c8_part3.txt"
foreach sample in  " non_han_hh == 1 " {
foreach y_var in  "elig_edu""educ" {
reghdfe `y_var' c.(fine_6_20) if  `sample' [aw = wt], a($CONTROL_MOTHER) cluster(prov)
su `y_var' if  `sample' [aw = wt]
outreg2 using "$path3/tab_c8_part3.xls", dec(3) append adds(mean, `r(mean)')
}
}


* Table C9, Rows I & J
cap erase "$path3/tab_c9_I_J.xls"
cap erase "$path3/tab_c9_I_J.txt"

foreach sample in  "han_hh == 1 & !rural" "han_hh == 1 & rural"{
foreach y_var in  "elig_edu""educ" {
reghdfe `y_var' c.(fine_6_20) if  `sample' [aw = wt], a($CONTROL_MOTHER) cluster(prov)
su `y_var' if  `sample' [aw = wt]
outreg2 using "$path3/tab_c9_I_J.xls", dec(3) append adds(mean, `r(mean)')

}
}



* Table C10
cap erase "$path3/tab_c10.xls"
cap erase "$path3/tab_c10.txt"
foreach sample in "han_hh == 1" {
reghdfe max_order c.(fine_0) if  `sample' &  b_order == 1 [aw = wt], a($CONTROL_GIRL) cluster(prov)
su max_order if  `sample' [aw = wt]
outreg2 using "$path3/tab_c10.xls", dec(3) append adds(mean, `r(mean)')

foreach y_var in "elig_edu""educ" {
reghdfe `y_var' c.(fine_0) if  `sample' [aw = wt], a($CONTROL_GIRL) cluster(prov)
su `y_var' if  `sample' [aw = wt]
outreg2 using "$path3/tab_c10.xls", dec(3) append adds(mean, `r(mean)')
}
}

log close 

